package as3.component.controls.tip
{
	
	import as3.component.FadeInOutSprite;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import gs.TweenMax;

	public class Tip extends FadeInOutSprite implements ITip
	{
		private var _type:uint; //提示的类型
		private var _msg:String=""; //提示文字
		private var _text:TextField;
		private var _textFormat:TextFormat;
		private var _position:Point; // 弹出的位置
		private var _msgChanged:Boolean;
		private static const BORDER_GAP:int=4;
		public static const TIP_WIDTH:uint=200;
		public static const TIP_HEIGHT:uint=80;
		public static const TIP_TYPE_COMMON:uint=1 //普通的提示
		public static const TIP_TYPE_ERROR:uint=2 //错误提示
		public static const TIP_TYPE_GAIN:uint=3 //获取物品提示


		public function Tip(tipMsg:String, position:Point=null)
		{
			super();
			drawBackGroud();
			_text=new TextField();
			_text.mouseEnabled=false;
			_text.selectable=false;
			_text.autoSize=TextFieldAutoSize.LEFT;
			addChild(_text);
			if (position)
				_position=position;
			type=TIP_TYPE_COMMON;
			msgString=tipMsg;

		}

		private function drawBackGroud():void
		{
			graphics.clear();
			graphics.beginFill(0xFF0000, 0);
			graphics.drawRect(0, 0, TIP_WIDTH, TIP_HEIGHT)
			graphics.endFill();
		}

		override protected function addToStageHandler(e:Event):void
		{
			//如果没有设置位置，则舞台居中
			if (!_position)
				_position=new Point((stage.stageWidth - width) >> 1, (stage.stageHeight - height) >> 1)
			x=_position.x
			y=_position.y
			super.addToStageHandler(e);
		}

		private function updateTextFormat(t:uint):void
		{
			switch (t)
			{
				case TIP_TYPE_COMMON:
					_textFormat=new TextFormat(null, 18, 0xFFFF00);
					break;
				case TIP_TYPE_ERROR:
					_textFormat=new TextFormat(null, 13, 0xFF0000);
					break;
				case TIP_TYPE_GAIN:
					_textFormat=new TextFormat(null, 13, 0x00FF00);
					break;
				default:
					_textFormat=new TextFormat(null, 13, 0xFFFF00);
					break;
			}
		}

		private function update():void
		{
			_text.defaultTextFormat=_textFormat;
			_text.text=_msg;
			if (!_msgChanged)
				return;
			if (_text.width > TIP_WIDTH - BORDER_GAP * 2)
			{
				_text.wordWrap=true;
				_text.width=TIP_WIDTH - BORDER_GAP * 2;
				//_text.height = TIP_HEIGHT - BORDER_GAP*2;
				_text.autoSize=TextFieldAutoSize.LEFT;
				_text.x=BORDER_GAP;
			}
			else
			{
				_text.x=Math.ceil(_text.x=(TIP_WIDTH - BORDER_GAP * 2 - _text.width) >> 1)
			}

			if (_text.height > TIP_HEIGHT - BORDER_GAP * 2)
			{
				_text.height=_text.height=TIP_HEIGHT - BORDER_GAP * 2;
				_text.y=BORDER_GAP;
			}
			else
			{
				_text.y=Math.ceil(_text.y=(TIP_HEIGHT - BORDER_GAP * 2 - _text.height) >> 1)
			}
			_msgChanged=false;

		}

		public function set msgString(msg:String):void
		{
			if (_msg == msg)
				return;
			_msgChanged=true;
			_msg=msg;
			update();
		}

		public function set bgUI(ui:DisplayObject):void
		{
			addChildAt(ui, 0);
		}

		public function set type(type:uint):void
		{
			if (_type == type)
				return;

			_type=type;

			updateTextFormat(_type);
			update();
		}
	}
}
